#!/bin/bash

# SCRIPT radios.sh
# Utilisation: "./radios.sh [adresse directe du flux radio]"
# Script proposant d'écouter des stations radio via mplayer
# Travail et idée originale de "Jotenakis"

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

REP_SHELL=`dirname $0`
source "$REP_SHELL/radios_config.sh"
source "$REP_SHELL/radios_common.sh"

#------------------------------------------------------------------------------#
# Gestion des paramètres			                               #
#------------------------------------------------------------------------------#
if [ $# -eq "$NO_ARGS" ]  # Script invoked with no command-line args?
then
	afficheLecteurAide;
	exit 0
fi  

TEMP=`getopt -o LVl:shcf: --long verbose-long,help-long,courant-long,list-long,lecture-long:,stop-long,fichier-long: -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around $TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
	log DEBUG "gestion de $@"
        case "$1" in
                -V|--verbose)
			DEBUG_MODE=$ALL;
			shift
			;;
		-f|--fichier-long)
                        log DEBUG "Option fichier, argument $2" 
            		FILE_CSV="$2";
			shift 2 
			;;
		
		-c|--courant)
			shift
			if [ -f "$PID_FILE" ]; then
				echo -e "RADIO \t: `cut -d';' -f2 $PID_FILE`";
				echo -e "URL \t: `cut -d';' -f3 $PID_FILE`";
				exit 0;
			fi
			;;
                
		-l|--lecture-long) 
			log DEBUG "Option lecture : $2" 
			LIST_RADIO="1"
			NUM_RADIO=$2
			shift 2
			;;
		
		-L|--list) 
			LIST_RADIO="1"
			shift 1
			;;
			
		-h|--help)
			afficheLecteurAide;
			exit 0
			;;
                -s|--stop-long) 
			log DEBUG "Option stop" 
			shift 
			STOP_RADIO="1";
			;;

                --) 
			shift
			log DEBUG "Option lecture sans option : $1" 
			if [ -n "$1" ]; then
				LANCE_RADIO="1"
				NUM_RADIO="$1"
			fi
			shift
			break
			;;
      		*)
      			log WARN "erreur interne $@"
      			exit 1; 
			;;
	esac
done
if [ -n "$LIST_RADIO" ]; then 
	listRadio
fi

if [ -n "$LANCE_RADIO" ]; then 
	lanceRadio $NUM_RADIO
	LANCE_RADIO=""
	NUM_RADIO=""
fi
if [ -n "$STOP_RADIO" ]; then 
	stopRadio 
fi

exit $?;
